package cn.uexpo.lhz.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;


import cn.uexpo.lhz.biz.DictionaryBiz;
import cn.uexpo.lhz.common.oldBase.BOException;
import cn.uexpo.lhz.common.oldUtils.PageRequest;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.common.oldUtils.PageUtils;
import cn.uexpo.lhz.dao.DictionaryItemMapper;
import cn.uexpo.lhz.dao.DictionaryMapper;
import cn.uexpo.lhz.domain.Dictionary;
import cn.uexpo.lhz.domain.DictionaryExample;
import cn.uexpo.lhz.domain.DictionaryItem;
import cn.uexpo.lhz.domain.DictionaryItemExample;

@Transactional
@Service
public class DictionaryBizImpl implements DictionaryBiz {

	@Autowired
	private DictionaryMapper dictionaryMapper;
	@Autowired
	private DictionaryItemMapper dictionaryItemMapper;

	@Override
	public int insert(Dictionary record) {
		// TODO Auto-generated method stub
		return dictionaryMapper.insert(record);
	}

//	@Override
//	public int delete(Dictionary record) {
//		// TODO Auto-generated method stub
//		return dictionaryMapper.deleteByPrimaryKey(record.getId());
//	}

	@Override
	public int delete(List<Dictionary> records) throws BOException {
		// TODO Auto-generated method stub
		for (Dictionary record : records) {
			dictionaryMapper.deleteByPrimaryKey(record.getId());
		}
		return 1;
	}

//	@Override
//	public int update(Dictionary record) throws BOException {
//		// TODO Auto-generated method stub
//		return dictionaryMapper.updateByPrimaryKey(record);
//	}

	@Override
	public int updateByPrimaryKeySelective(Dictionary record) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public Dictionary selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return dictionaryMapper.selectByPrimaryKey(id);
	}

	@Override
	public long countByExample(DictionaryExample example) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.countByExample(example);
	}

	@Override
	public List<Dictionary> selectByExample(DictionaryExample example) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.selectByExample(example);
	}

	@Override
	public PageResult findPage(PageRequest pageRequest, DictionaryExample example) throws BOException {

		int pageNum = pageRequest.getPage();
		int pageSize = pageRequest.getLimit();
		PageHelper.startPage(pageNum, pageSize);
		List<Dictionary> records = (List<Dictionary>) dictionaryMapper.selectByExample(example);
		return PageUtils.getPageResult(pageRequest, new PageInfo<Dictionary>(records));
	}

	@Override
	public Integer deleteDictionaryByDictCode(String dicCode) throws BOException {
		// 先删除子表
		DictionaryItemExample example = new DictionaryItemExample();
		DictionaryItemExample.Criteria criteria = example.createCriteria();
		criteria.andDictcodeEqualTo(dicCode);
		List<DictionaryItem> items = dictionaryItemMapper.selectByExample(example);
		if (items != null && items.size() > 0) {
			for (DictionaryItem dictionaryItem : items) {
				dictionaryItemMapper.deleteByPrimaryKey(dictionaryItem.getId());
			}
		}
		// 再删除主表
		DictionaryExample e = new DictionaryExample();
		DictionaryExample.Criteria c = e.createCriteria();
		c.andDictcodeEqualTo(dicCode);
		List<Dictionary> records = dictionaryMapper.selectByExample(e);
		int no = 0;
		if (records != null && records.size() > 0) {
			Dictionary record = (Dictionary) records.get(0);
			no = dictionaryMapper.deleteByPrimaryKey(record.getId());
		}

		return no;
	}


	@Override
	public List<DictionaryItem> selectDictItmesByDictCode(String dictCode) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryItemMapper.selectDictItmesByDictCode(dictCode);
	}

	@Override
	public DictionaryItem selectDictItmeByDicCodeItemCode(String dictCode, String itemCode) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryItemMapper.selectDictItmeByDicCodeItemCode(dictCode, itemCode);
	}

	@Override
	public String selectValueBy(String dictCode, String itemCode) throws BOException {
		String value = "";
		DictionaryItem item = selectDictItmeByDicCodeItemCode(dictCode, itemCode);
		if (item != null) {
			value = item.getValue();
		}
		// TODO Auto-generated method stub
		return value;
	}

	@Override
	public String selectNameBy(String dictCode, String itemCode) throws BOException {
		String name = "";
		DictionaryItem item = selectDictItmeByDicCodeItemCode(dictCode, itemCode);
		if (item != null) {
			name = item.getName();
		}
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public List<JSONObject> selectJsonByDictCode(String dictCode) throws BOException {

		List<JSONObject> list = new ArrayList<JSONObject>();
		List<DictionaryItem> items = selectDictItmesByDictCode(dictCode);
		for (DictionaryItem item : items) {
			JSONObject obj = new JSONObject();
			obj.put("code", item.getItemcode());
			obj.put("name", item.getValue());
			list.add(obj);
		}
		return list;
	}

	@Override
	public List<JSONObject> selectJsonByDictCode(String dictCode, String keyCode, String nameCode)
			throws BOException {
		List<JSONObject> list = new ArrayList<JSONObject>();
		List<DictionaryItem> items = selectDictItmesByDictCode(dictCode);
		for (DictionaryItem item : items) {
			JSONObject obj = new JSONObject();
			obj.put(keyCode, item.getItemcode());
			obj.put(nameCode, item.getValue());
			list.add(obj);
		}
		return list;
	}

	@Override
	public Dictionary selectByUniqueCode(String uniqueCode) throws BOException {
		Dictionary item=null;
		DictionaryExample example=new DictionaryExample();
		DictionaryExample.Criteria citeria=example.createCriteria();
		 citeria.andDictcodeEqualTo(uniqueCode);
		 List<Dictionary> items=dictionaryMapper.selectByExample(example);
		 if (items!=null&&items.size()>0) {
			 item=items.get(0);
		}
		return item;
	}

	@Override
	public List<JSONObject> selectJsonInitFalg(String dictCode, String id, String text)
			throws BOException {
		List<JSONObject> list = new ArrayList<JSONObject>();
		List<DictionaryItem> items = selectDictItmesByDictCode(dictCode);
		JSONObject objInit = new JSONObject();
		objInit.put(id, "");
		objInit.put(text, "请选择");
		list.add(objInit);
		for (DictionaryItem item : items) {
			JSONObject obj = new JSONObject();
			obj.put(id, item.getItemcode());
			obj.put(text, item.getValue());
			list.add(obj);
		}
		return list;
	}

	@Override
	public List<String> selectDistinctTypes() throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.selectDistinctTypes();
	}

	@Override
	public List<Dictionary> selectByType(String type) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.selectByType(type);
	}


//	=================================================================================================================================================
	@Override
	public int insertSelective(Dictionary record) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.insertSelective(record);
	}

	@Override
	public int deleteByPrimaryKey(Integer id) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteEntity(Dictionary record) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.deleteByPrimaryKey(record.getId());
	}

	@Override
	public int deleteByIds(List<Integer> ids) throws BOException {
		// TODO Auto-generated method stub
		for (Integer id : ids) {
			dictionaryMapper.deleteByPrimaryKey(id);
		}
		return 1;
	}

	@Override
	public int deleteByExample(DictionaryExample example) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.deleteByExample(example);
	}

	@Override
	public int updateByPrimaryKey(Dictionary record) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.updateByPrimaryKey(record);
	}

	@Override
	public int updateByExampleSelective(Dictionary record, DictionaryExample example) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.updateByExampleSelective(record,example);
	}

	@Override
	public int updateByExample(Dictionary record, DictionaryExample example) throws BOException {
		// TODO Auto-generated method stub
		return dictionaryMapper.updateByExample(record,example);
	}








}
